package com.cat.recruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cat.recruit.bean.dto.AccessDTO;
import com.cat.recruit.bean.dto.AppointmentSlotDTO;
import com.cat.recruit.bean.dto.ScoreCommentDTO;
import com.cat.recruit.bean.dto.UserDTO;
import com.cat.recruit.entity.Access;
import com.cat.recruit.entity.AppointmentSlot;
import com.cat.recruit.entity.ScoreComment;
import com.cat.recruit.entity.User;
import com.cat.recruit.mapper.AppointmentSlotMapper;
import com.cat.recruit.mapper.ScoreCommentMapper;
import com.cat.recruit.mapper.UserMapper;
import com.cat.recruit.service.CommonService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    CacheManager cacheManager;
    @Autowired
    private AppointmentSlotMapper appointmentSlotMapper;
    @Autowired
    private ScoreCommentMapper scoreCommentMapper;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    //@Cacheable(value = "user", key = "'user::id::'+#userId")
    public User getUserByUserId(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public UserDTO getUserDtoByUserId(Integer userId) {
        // 先从缓存中获取用户信息
        User user = null;
        // 获取缓存管理器
        //Cache cache = cacheManager.getCache("user");
        //if (cache != null) {
        //    String key = "user::id::"+userId;
        //    user =cache.get(key, User.class);
        //}
        if (user == null){
            user = userMapper.selectById(userId);
        }
        return new UserDTO(user);
    }

    //@Cacheable(value = "appointmentSlot", key = "'appointmentSlotDto::id::'+#id")
    @Override
    public AppointmentSlotDTO getAppointmentSlotDtoById(Integer id) {
        AppointmentSlot appointmentSlot = appointmentSlotMapper.selectById(id);
        return new AppointmentSlotDTO(appointmentSlot);
    }

    /**
     * 生成AccessDTO
     * @param access 考核实体
     * @return AccessDTO
     */
    @Override
    public AccessDTO getAccessDto(Access access) {
        // 查询用户DTO
        UserDTO studentDTO = getUserDtoByUserId(access.getUserId());
        // 查询评分评论列表
        List<ScoreComment> scoreCommentList = scoreCommentMapper.selectList(
                new QueryWrapper<ScoreComment>()
                        .eq("access_id",access.getAccessId())
                        .eq("state",0)
        );
        List<ScoreCommentDTO> scoreCommentDTOList = scoreCommentList.stream()
                .map(ScoreCommentDTO::new)
                .toList();
        return  new AccessDTO(access,studentDTO,scoreCommentDTOList);
    }
}